﻿using System;
using PowerToolsWeb.Entities;
using System.Text;
using System.Web.UI;
namespace PowerToolsWeb.Facade
{
    /// <summary>
    /// 后台管理页面都继承此类
    /// </summary>
    public class AdminPageBase : System.Web.UI.Page
    {
        public SystemUser UserInfo
        {
            get
            {
                if (Session["SystemUser"] != null)
                    return (SystemUser)Session["SystemUser"];
                else
                    return null;
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            if (UserInfo == null)
                Response.Redirect("../Login.aspx?url=Admin/Default.aspx");
            base.OnLoad(e);
        }
        #region Metho
        /// <summary>
        /// 弹出客户端警告对话框。模拟javascript的alert
        /// </summary>
        /// <param name="Page">当前的页面的 Page 类，在调用时用 Page 就行了</param>
        /// <param name="msg">要弹出的字符串</param>
        /// <returns> 无返回值</returns>
        public static void Alert(Page Mypage, string msg)
        {
            if (msg == null) return;
            string tempstr = msg.Replace("\"", "\\\"");
            tempstr = tempstr.Replace("\'", "\\\'").Replace("\n", "").Replace("\r", "");
            StringBuilder sb = new StringBuilder("<script Language=\"Javascript\">");
            sb.Append("alert(\"");
            sb.Append(tempstr).Append("\");");
            sb.Append("<").Append("/").Append("script>");

            if (!Mypage.ClientScript.IsStartupScriptRegistered("msgscript"))
                Mypage.ClientScript.RegisterStartupScript(Mypage.GetType(), "msgscript", sb.ToString());
        }

        /// <summary>
        /// 重载弹出信息功能，并重定向
        /// </summary>
        /// <param name="Mypage"></param>
        /// <param name="msg"></param>
        /// <param name="RedirectURL">要定向的页面，如果为空，则到默认主页</param>
        public static void Alert(Page Mypage, string msg, string RedirectURL)
        {
            if (msg == null) return;
            string tempstr = msg.Replace("\"", "\\\"");
            tempstr = tempstr.Replace("\'", "\\\'").Replace("\n", "").Replace("\r", "");
            StringBuilder sb = new StringBuilder("<script Language=\"Javascript\">");
            sb.Append("alert(\"");
            sb.Append(tempstr).Append("\");");
            sb.Append("document.location='" + RedirectURL + "'"); //要定向的页面，如果为空，则到默认主页
            sb.Append("<").Append("/").Append("script>");
            if (!Mypage.ClientScript.IsStartupScriptRegistered("msgscript"))
                Mypage.ClientScript.RegisterStartupScript(Mypage.GetType(), "msgscript", sb.ToString());
        }
        /// <summary>
        /// 运行一段JavaScript 代码。
        /// </summary>
        /// <param name="Mypage">当前的页面的 Page 类，在调用时用 Page 就行了</param>
        /// <param name="script">要运行的 JavaScript 代码</param>
        public static void RunJs(Page Mypage, string script)
        {
            if (script == null) return;
            StringBuilder sb = new StringBuilder("<script Language=\"Javascript\">");
            sb.Append(script);
            sb.Append("</script>");

            if (!Mypage.ClientScript.IsStartupScriptRegistered("msgscript"))
                Mypage.ClientScript.RegisterStartupScript(Mypage.GetType(), "msgscript", sb.ToString());
        }
        /// <summary>
        /// 网站的路径，包含虚拟目录路径
        /// 格式: 协议 + "://" + 域名 (+ ":" + 端口) (+ 虚拟目录路径)
        /// 不以"/"结束
        /// </summary>
        public string SiteUrl
        {
            get
            {
                if (Request.ApplicationPath == "/")
                    return Request.Url.Scheme + "://" + Request.Url.Authority;
                else
                    return Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath;
            }

        }
        #endregion
    }
}
